<?php
/**********************************************************
  eCode Marketing System
  Version 0.1
  Add Offer Form (/admin/offer_add.php)
**********************************************************/

// Require configuration
define("FROMSCRIPT", true);
require dirname(__FILE__) . "/../config.php";


// Start session if not already started
session_start();

if(!isset($_SESSION['login_t'])) {

	/* If we're not logged in, take us to the login page and
	ensure the rest of the code isn't run */

	header("Location: /admin/login.php");
	exit;
}

// Extra jQuery for the preview function
$extra_jQuery = "$(\"#preview\").click(function(){\n
var offer_line1 = $(\"input#offer_line1\").val();
var offer_line2 = $(\"input#offer_line2\").val();
var offer_line3 = $(\"input#offer_line3\").val();
var offer_type = $(\"select#offer_type\").val();
var offer_price = $(\"input#offer_price\").val();
var offer_value = $(\"input#offer_value\").val();
var offer_pos_code = $(\"input#offer_pos_code\").val();
var campaign_id = $(\"select#campaign_id\").val();
var imgTag = '<img src=\\\"offer_preview.php?offer_line1=' + offer_line1 +
'&offer_line2=' + offer_line2 +
'&offer_line3=' + offer_line3 +
'&offer_type=' + offer_type +
'&offer_price=' + offer_price +
'&offer_value=' + offer_value +
'&offer_pos_code=' + offer_pos_code +
'&campaign_id=' + campaign_id +
'\\\" alt=\\\"(Preview image.)\\\" />';
$(\"#preview-box\").fadeIn('slow');
$(\"#preview-img\").html(imgTag);
});

$(\"#preview-close\").click(function(){
$(\"#preview-box\").fadeOut('slow');
});";

$pagetitle = "Add Offer (Admin)";
$pageheading = "<a href=\"/admin/index.php\">" . SITE_NAME . " Administration</a> &gt; Add Offer";
$page_content = "<hr />\n";
	
// Generate editing form

// Get campaign information from database
$campaigns = $db->get_results("SELECT * FROM `" . DB_TBL_PREFIX . "campaigns`");

$page_content .= "<form action=\"handler.offer_add.php\" method=\"post\">\n";
$page_content .= "<table class=\"table-60\">\n";
$page_content .= "<tr><td><label for=\"campaign_id\">Campaign:</label></td><td>";
$page_content .= "<select name=\"campaign_id\" id=\"campaign_id\">\n";
$page_content .= "<option>(Select campaign)</option>\n";

foreach( $campaigns as $campaign ) {
	$page_content .= "<option value=\"" . $campaign->campaign_id . "\">" . $campaign->campaign_id .  " - " . $campaign->campaign_name . " (" . $campaign->start_date . " until " . $campaign->end_date . ")</option>\n";
}

$page_content .= "</select>\n";
$page_content .= "</td></tr>\n";
$page_content .= "<tr><td><label for=\"offer_line1\">Offer Line 1<br />(32 characters max.)</label></td><td><input type=\"text\" name=\"offer_line1\" id=\"offer_line1\" size=\"32\" maxlength=\"32\" /></td></tr>\n";
$page_content .= "<tr><td><label for=\"offer_line2\">Offer Line 2<br />(32 characters max.)</label></td><td><input type=\"text\" name=\"offer_line2\" id=\"offer_line2\" size=\"32\" maxlength=\"32\" /></td></tr>\n";
$page_content .= "<tr><td><label for=\"offer_line2\">Offer Line 3<br />(32 characters max.)</label></td><td><input type=\"text\" name=\"offer_line3\" id=\"offer_line3\" size=\"32\" maxlength=\"32\" /></td></tr>\n";
$page_content .= "<tr><td><label for=\"offer_pos_code\">Offer POS Code<br />(4 digits max.)</label></td><td># <input type=\"text\" name=\"offer_pos_code\" id=\"offer_pos_code\" size=\"4\" maxlength=\"4\" /></td></tr>\n";
$page_content .= "<tr><td><label for=\"offer_type\">Offer Type</label></td><td>";
$page_content .= "<select name=\"offer_type\" id=\"offer_type\">\n";

$offer_types = $db->get_results("SELECT * FROM `" . DB_TBL_PREFIX . "offertypes`");
foreach( $offer_types as $offer_type ) {
	$page_content .= "<option value=\"" . $offer_type->offer_type_id . "\">" . $offer_type->offer_type_id .  " - " . $offer_type->offer_type_name . "</option>\n";
}

$page_content .= "</select>\n";
$page_content .= "</td></tr>\n";
$page_content .= "<tr><td><label for=\"offer_price\">Offer Amount<br />($ or % automatically added)</label></td><td><input type=\"text\" name=\"offer_price\" id=\"offer_price\" /></td></tr>\n";
$page_content .= "<tr><td><label for=\"offer_value\">Offer Value<br />(types 0, 3, and 4)</label></td><td>$ <input type=\"text\" name=\"offer_value\" id=\"offer_value\" /></td></tr>\n";
$page_content .= "<tr><td class=\"centered\" colspan=\"2\"><input type=\"submit\" name=\"action\" id=\"preview\" value=\"Preview\" onClick=\"return false;\" />&nbsp;<input type=\"submit\" name=\"action\" value=\"Save\" /></td></tr>\n";
$page_content .= "</table>\n";
$page_content .= "</form>\n";

$page_content .= "<div id=\"preview-box\">\n";
$page_content .= "<h3>This is a preview of how the coupon will look in the user interface.</h3>\n";
$page_content .= "<p>Your changes have not been committed yet. To commit your changes, click \"Save\" above.</p>\n";
$page_content .= "<div id=\"preview-img\"></div>\n";
$page_content .= "<p><a href=\"javascript:return false;\" id=\"preview-close\">Close</a></p>\n";
$page_content .= "</div>\n";

$page_content .= "<hr />\n";
$page_content .= "<p><a href=\"/admin/index.php\">Back</a> to " . SITE_NAME . " Administration</p>\n";

// Require template
require TEMPLATE_DIR . "/index.php";
?>